package com.woniu.account.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.account.dto.admin.AdminDto;
import com.woniu.account.form.admin.AddAdminParam;
import com.woniu.account.model.Admin;
import com.woniu.account.dao.AdminDao;
import com.woniu.account.param.admin.AdminLoadParam;
import com.woniu.account.param.admin.AdminLoginParam;
import com.woniu.account.service.AdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.exception.admin.AdminException;
import com.woniu.exception.admin.AdminExceptionCode;
import com.woniu.util.MD5Util;
import com.woniu.util.StateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 杜岷芫
 * @since 2024年07月11日
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminDao, Admin> implements AdminService {
    @Autowired
    private AdminDao adminDao;
    @Autowired
    private MD5Util md5Util;

    @Override
    public AdminDto login(AdminLoginParam param) {
        QueryWrapper<Admin> qw = new QueryWrapper<Admin>().eq("admin_phone", param.getAdminName())
                .or().eq("admin_name", param.getAdminName())
                .or().eq("admin_nickname", param.getAdminName());
        Admin admin = adminDao.selectOne(qw);
        if (ObjectUtil.isEmpty(admin))
            throw new AdminException(AdminExceptionCode.ADMIN_NOT_EXIST);
        if (admin.getAdminState() != StateUtil.VENUE_ADMIN_STATE_NORMAL)
            throw new AdminException(AdminExceptionCode.ADMIN_ABNORMAL);
        if (!md5Util.encryption(admin.getAdminPhone(), param.getAdminPass()).equals(admin.getAdminPassword()))
            throw new AdminException(AdminExceptionCode.ADMIN_ERROR);
        AdminDto adminDto = BeanUtil.toBean(admin, AdminDto.class);
        return adminDto;
    }

    @Override
    public PageInfo<AdminDto> load(AdminLoadParam param) {
        PageHelper.startPage(param.getPageNum() == null ? 1 : param.getPageNum(), param.getPageSize() == null ? 6 : param.getPageSize());
        QueryWrapper<Admin> qw = new QueryWrapper<>();
        if (ObjectUtil.isNotEmpty(param.getAdminName()))
        qw.and(q->q.like("admin_phone", param.getAdminName())
                .or().like("admin_name", param.getAdminName())
                .or().like("admin_nickname", param.getAdminName()));
        if (ObjectUtil.isNotEmpty(param.getAdminState()))
            qw.and(q -> q.eq("admin_state", param.getAdminState()));
        if (ObjectUtil.isNotEmpty(param.getAdminRole()))
            qw.eq("admin_role_id", param.getAdminRole());
        ArrayList<AdminDto> adminDtos = adminDao.load(param);
        PageInfo<AdminDto> adminDtoPageInfo = new PageInfo<>(adminDtos);
        return adminDtoPageInfo;
    }

    @Override
    public void changeState(Integer adminId, Integer state) {
        Admin admin = adminDao.selectById(adminId);
        if (ObjectUtil.isEmpty(admin))
            throw new AdminException(AdminExceptionCode.ADMIN_NOT_EXIST);
        if (admin.getAdminState() == state || admin.getAdminState() == StateUtil.VENUE_ADMIN_STATE_DELETE)
            throw new AdminException(AdminExceptionCode.ADMIN_ABNORMAL);
        admin.setAdminState(state);
        adminDao.updateById(admin);
    }

    @Override
    public AdminDto addAdmin(AddAdminParam param) {
        //验证账号名是否重复
        QueryWrapper<Admin> qw = new QueryWrapper<Admin>();
        qw.eq("admin_phone", param.getAdminPhone());
        Admin admin = adminDao.selectOne(qw);

        if (ObjectUtil.isNotEmpty(admin))
            throw new AdminException(AdminExceptionCode.ADMIN_PHONE_HAD_EXIST);
        qw.eq("admin_nickname", param.getAdminNickname());
        Admin admin1 = adminDao.selectOne(qw);
        if (ObjectUtil.isNotEmpty(admin1))
            throw new AdminException(AdminExceptionCode.ADMIN_NICKNAME_HAD_EXIST);
        //新增
        //实现添加管理员
        Admin admin2 = BeanUtil.toBean(param, Admin.class);
        admin2.setAdminPassword(md5Util.encryption(admin2.getAdminPhone(), "123456"));
        adminDao.insert(admin2);
        AdminDto adminDto = BeanUtil.toBean(admin2, AdminDto.class);
        return adminDto;
    }
}

